home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 351-375 / 351 / pdc / pdcsrc.lzh / PDC / GetOpt.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  3KB  |  101 lines

  1.  
  2. /* PDC Compiler - A Freely Distributable C Compiler for the Amiga
  3.  *                Based upon prior work by Matthew Brandt and Jeff Lydiatt.
  4.  *
  5.  * PDC Compiler release 3.3 Copyright (C) 1989 Paul Petersen and Lionel Hummel.
  6.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  7.  *
  8.  * This code is freely redistributable upon the conditions that this 
  9.  * notice remains intact and that modified versions of this file not be 
  10.  * distributed as part of the PDC Software Distribution without the express
  11.  * consent of the copyright holders.
  12.  *
  13.  *------------------------------------------------------------------
  14.  *
  15.  * $Log:    GetOpt.c,v $
  16.  * Revision 3.33  90/04/05  22:34:24  lionel
  17.  * None.
  18.  * 
  19.  * Revision 3.32  90/02/03  16:24:27  lionel
  20.  * None
  21.  * 
  22.  *------------------------------------------------------------------
  23.  */
  24.  
  25. /*
  26.  * GetOpt.c Parse command line options
  27.  * 
  28.  * Modification history (LDH): optsign variable added, along with the ability to
  29.  * distinguish between '+arg' and '-arg'
  30.  */
  31.  
  32. #include    <stdio.h>
  33.  
  34. #define ERR(spat, cpat)   \
  35.     if (opterr) {   errbuf[0] = cpat; errbuf[1] = '\n';     \
  36.     (void) fwrite( argv[0], 1, strlen(argv[0]), stderr);    \
  37.     (void) fwrite( spat, 1, strlen(spat), stderr);              \
  38.     (void) fwrite( errbuf, 1, 2, stderr); }
  39.  
  40. extern int      strcmp();
  41. extern char    *index();
  42. extern int      strlen();
  43.  
  44. int             opterr = 1;
  45. int             optind = 1;
  46. int             optopt;
  47. char            optsign;
  48. char           *optarg;
  49.  
  50. int
  51. getopt(argc, argv, opts)
  52.     int             argc;
  53.     char          **argv, *opts;
  54. {
  55.     static int      sp = 1;
  56.     register int    c;
  57.     register char  *cp;
  58.     char            errbuf[2];
  59.  
  60.     if (sp == 1) {
  61.         if (optind >= argc)
  62.             return( EOF );
  63.         optsign = *argv[optind];
  64.         if ( (optsign != '-' && optsign != '+') || argv[optind][1] == '\0')
  65.             return (EOF);
  66.         else if (strcmp(argv[optind], "--") == NULL) {
  67.             optind++;
  68.             return (EOF);
  69.         }
  70.     }
  71.     optopt = c = argv[optind][sp];
  72.     if (c == ':' || (cp = index(opts, c)) == NULL) {
  73.         ERR(": illegal option -- ", c);
  74.         if (argv[optind][++sp] == '\0') {
  75.             optind++;
  76.             sp = 1;
  77.         }
  78.         return ('?');
  79.     }
  80.     if (*++cp == ':') {
  81.         if (argv[optind][sp + 1] != '\0')
  82.             optarg = &argv[optind++][sp + 1];
  83.         else if (++optind >= argc) {
  84.             ERR(": option requires an argument -- ", c);
  85.             sp = 1;
  86.             return ('?');
  87.         }
  88.         else
  89.             optarg = argv[optind++];
  90.         sp = 1;
  91.     }
  92.     else {
  93.         if (argv[optind][++sp] == '\0') {
  94.             sp = 1;
  95.             optind++;
  96.         }
  97.         optarg = NULL;
  98.     }
  99.     return (c);
  100. }
  101.